<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <link rel="stylesheet" href="/static/lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
    <style>
        .layui-form-item {
            margin-top: 10px;
            margin-right: 30px;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form layuimini-container">
    <div class="layui-form-item">
        <label class="layui-form-label ">标题</label>
        <div class="layui-input-block">
            <input type="text" name="pageTitle" placeholder="请输入文章标题" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <table class="layui-table" id="table" lay-filter="table">
            <thead>
            <tr>
                <td>标题</td>
                <td>图标</td>
                <td>类型</td>
                <td>是否显示边框</td>
                <td>url</td>
                <td>setting</td>
                <td><a class="layui-btn layui-btn-xs add">添加</a></td>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td><input type="text"  class="layui-input" name="rows[title][]"></td>
                <td><input type="text"  class="layui-input" name="rows[icon][]"></td>
                <td><input type="text"  class="layui-input" name="rows[type][]"></td>
                <td><input type="text"  class="layui-input" name="rows[showBorder][]"></td>
                <td><input type="text"  class="layui-input" name="rows[url][]"></td>
                <td><input type="text"  class="layui-input" name="rows[setting][]"></td>
                <td>
                    <a class="layui-btn layui-btn-danger layui-btn-xs del">删除</a>
                </td>
            </tr>
            </tbody>
        </table>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
        </div>
    </div>
</div>

<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>



    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                type: 'POST',
                url: '/admin/panel/updateData',
                dataType: 'json',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                data: data.field,
                success: function (data) {
                    if (data.status == 1) {
                        layer.alert(data.message, {icon: 6}, function () {
                            parent.location.reload(true);
                        });
                    } else {
                        layer.alert(data.message, {icon: 5});
                    }
                },
                error: function () {
                    //错误信息
                }

            });
            return false;
        });
        //因为动态添加的元素class属性是无效的，所以不能用$('.add').click(function(){});
        $('body').on('click', '.add', function () {
            //你要添加的html
            var html = '<tr>' +
                ' <td><input type="text"  class="layui-input" name="rows[title][]"></td>' +
                ' <td><input type="text"  class="layui-input" name="rows[icon][]"></td>' +
                ' <td><input type="text"  class="layui-input" name="rows[type][]"></td>' +
                ' <td><input type="text"  class="layui-input" name="rows[showBorder][]"></td>' +
                ' <td><input type="text"  class="layui-input" name="rows[url][]"></td>' +
                ' <td><input type="text"  class="layui-input" name="rows[setting][]"></td>' +
                '<td>' +
                '<a class="layui-btn layui-btn-danger layui-btn-xs del">删除</a>' +
                '</td>' +
                '</tr>';
            //添加到表格最后
            $(html).appendTo($('#table tbody:last'));

        });

        $('body').on('click', '.del', function () {
            if ($('#table tbody tr').length === 1) {
                layer.msg('只有一条不允许删除。', {
                    time: 2000
                });
            } else {
                //删除当前按钮所在的tr
                $(this).closest('tr').remove();
            }
        });
    });
</script>
</body>
</html>